home *** CD-ROM | disk | FTP | other *** search
/ AP Professional Graphics CD-ROM Library / AP Professional Graphics CD-ROM Library.iso / pc / pc / appendix / gemsi / pixelint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-15  |  1.5 KB  |  56 lines

  1. /*
  2. Proper Treatment of Pixels as Integers
  3. by Alan Paeth
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. #define Min     code[2]
  8. #define Med     code[1]
  9. #define Max     code[0]
  10. #define NCODE   3
  11.  
  12. #include <string.h>    /* or <strings.h> */
  13.  
  14. /*
  15.  * A call to getplanes of the form:
  16.  * getplanes(&red, &green, &blue, 256, "grb");
  17.  *
  18.  * fills the first three integer pointers with (near) identical
  19.  * values which maximize red*green*blue <= 256. The final parameter
  20.  * string defines tie-break order, here green>=red>=blue (the usual
  21.  * default). The present code procedure calls "err(string, arg)"
  22.  * given bad parameters; it is a simple task to rewrite the code as
  23.  * a function which returns a success/failure code(s), as needed.
  24.  *
  25.  * In the example given above the code fills in the values
  26.  * red = 6, green = 7, blue = 6.
  27.  */
  28.  
  29. getplanes(r, g, b, n, bias)
  30.     int *r, *g, *b;
  31.     char *bias;
  32.     {
  33.     int i, code[NCODE];
  34.     if(strlen(bias) != NCODE )
  35.         err("bias string \"%s\" wrong length",bias);
  36.     Min = Med = Max = 0;
  37.     *r = *g = *b = 0;
  38.     while(Min*Min*Min <= n) Min++;
  39.     Min--;
  40.     while(Med*Med*Min <= n) Med++;
  41.     Med--;
  42.     Max = n/(Min*Med);
  43.     for( i = 0; i < NCODE; i++ )
  44.        {
  45.         switch(bias[i])
  46.             {
  47.             case 'r': case 'R': *r = code[i]; break;
  48.             case 'g': case 'G': *g = code[i]; break;
  49.             case 'b': case 'B': *b = code[i]; break;
  50.             default: err("bad bias character: \'%c\'",bias[i]); break;
  51.             }
  52.         }
  53.     if (!(*r && *g && *b)) err("bias string \"%s\" deficient", bias);
  54.     }
  55.     
  56.